﻿$(start);

var opurl = "service/tagerOp.ashx";
var bd = null;
var disp = null;

function start() {
	bd = $('body');
	attach();

	for(var ti in test) {
		test[ti]();
	}

	dispVcode();

	// disp
	bd.append('<div class="disp" />');
	disp = $('.disp');

	// trigger
	// 全部关闭
	$('.manager .title').trigger('click');
	// 要被打开的节点
	var openNodeName = 'finder';
	$('.manager[ptag="' + openNodeName + '"] .title').trigger('click');
}

function dispVcode() {
	var vcodeNode = $('<div/>').css({
		position: 'fixed',
		top: '120px',
		right: '180px',
		cursor: 'pointer'
	});

	var imgVcode = $('<img />').click(refresh);


	vcodeNode.append(imgVcode);

	imgVcode.trigger('click');
	bd.append(vcodeNode);

	function refresh() {
		var rnd = Math.random();
		imgVcode.attr('src', 'service/vcode.ashx?rnd=' + rnd);
	}
}

var test = {
	bag: function() {
		// bag
		create({
			manager: 'bag',
			func: 'getBags'
		}, ['keyWord',
		{
			name: 'pageIndex',
			val: 0
		}, {
			name: 'pageSize',
			val: 8
		}]);

		create({
			manager: 'bag',
			func: 'getFrozenBags'
		}, ['keyWord',
		{
			name: 'pageIndex',
			val: 0
		}, {
			name: 'pageSize',
			val: 8
		}]);

		create({
			manager: 'bag',
			func: 'insertBag'
		}, ['name']);

		create({
			manager: 'bag',
			func: 'editBag'
		}, ['bagId', 'status']);

		create({
			manager: 'bag',
			func: 'deleteBag'
		}, ['bagId']);
	},
	tag: function() {
		// tag
		create({
			manager: 'tag',
			func: 'getAllTags'
		}, []);

		create({
			manager: 'tag',
			func: 'getTagsByBagIds'
		}, ['bagIds',
		{
			name: 'pageIndex',
			val: 0
		}, {
			name: 'pageSize',
			val: 8
		}]);


		create({
			manager: 'tag',
			func: 'getFrozenTags'
		}, ['bagIds',
		{
			name: 'pageIndex',
			val: 0
		}, {
			name: 'pageSize',
			val: 8
		}]);

		create({
			manager: 'tag',
			func: 'getHotTags'
		}, [{
			name: 'count',
			val: '5'
		}]);

		create({
			manager: 'tag',
			func: 'insertTag'
		}, [{
			name: 'bagId',
			val: '6'
		}, 'name']);

		create({
			manager: 'tag',
			func: 'editTag'
		}, ['tagId', 'status']);

		create({
			manager: 'tag',
			func: 'deleteTag'
		}, ['tagId']);

	},
	video: function() {
		// video
		create({
			manager: 'video',
			func: 'insertVideo'
		}, ['name',
		{
			name: 'url',
			val: 'www.sina.com.cn'
		}, 'tagIds']);

		create({
			manager: 'video',
			func: 'editVideoContent'
		}, ['videoId', 'name',
		{
			name: 'url',
			val: 'www.sina.com.cn'
		}]);


		create({
			manager: 'video',
			func: 'editVideo'
		}, ['videoId', 'status']);


		create({
			manager: 'video',
			func: 'deleteVideo'
		}, ['videoIds']);


		create({
			manager: 'video',
			func: 'search'
		}, ['tagIds',
		{
			name: 'pageIndex',
			val: 0
		}, {
			name: 'pageSize',
			val: 8
		}]);


		create({
			manager: 'video',
			func: 'searchFrozen'
		}, [{
			name: 'pageIndex',
			val: 0
		}, {
			name: 'pageSize',
			val: 8
		}]);

		create({
			manager: 'video',
			func: 'addVideoTags'
		}, ['videoId', 'tagIds']);

		create({
			manager: 'video',
			func: 'removeVideoTags'
		}, ['videoId', 'tagIds']);

	},
	finder: function() {
		create({
			manager: 'finder',
			func: 'insertFinder'
		}, ['name', 'tagIds']);

		create({
			manager: 'finder',
			func: 'editFinder'
		}, ['finderId', 'name', 'tagIds']);

		create({
			manager: 'finder',
			func: 'deleteFinder'
		}, ['finderId']);

		create({
			manager: 'finder',
			func: 'searchFinder'
		}, []);

		create({
			manager: 'finder',
			func: 'rankFinder'
		}, ['ids', 'ranks']);



	},
	user: function() {
		create({
			manager: 'user',
			func: 'reg'
		}, [{
			name: 'userName',
			val: '1328192478@qq.com'
		}, {
			name: 'pwd',
			val: 'violet'
		}, 'vcode']);

		create({
			manager: 'user',
			func: 'login'
		}, [{
			name: 'userName',
			val: '1328192478@qq.com'
		}, {
			name: 'pwd',
			val: 'violet'
		}]);

		create({
			manager: 'user',
			func: 'logout'
		}, []);

		create({
			manager: 'user',
			func: 'changePwd'
		}, [{
			name: 'exPwd',
			val: 'violet'
		}, {
			name: 'newPwd',
			val: 'beibeizhu'
		}]);
	},
	syno: function() {
		create({
			manager: 'syno',
			func: 'insert'
		}, ['tagId', 'synoName']);

		create({
			manager: 'syno',
			func: 'edit'
		}, ['synoId', 'status']);

		create({
			manager: 'syno',
			func: 'delete'
		}, ['synoId']);

		create({
			manager: 'syno',
			func: 'getTagIdByName'
		}, ['synoName']);


		create({
			manager: 'syno',
			func: 'getSynosByTagId'
		}, ['tagId']);
	}
};



function create(testName, inputs) {
	var manager = null;
	if($('.manager[ptag="' + testName.manager + '"]').length == 0) {
		var node = $('<div class="manager" ptag="' + testName.manager + '" />');
		node.append(['<div class="title">', testName.manager, '</div>'].join(''));
		bd.append(node);

	}
	manager = $('.manager[ptag="' + testName.manager + '"]');
	var func = $('<div class="func" />')
	func.append(['<div class="funcName" ptag="' + testName.func + '" >', testName.func, '</div>'].join(''));

	$.each(inputs, function(i, n) {
		var name = null;
		var val = null;
		if(typeof(n) == "object") {
			name = n.name;
			val = n.val;
		} else {
			name = n;
			val = "";
		}
		var paramName = $('<div class="paramName" />').text(name);
		var mao = $('<div class="mao">:</div>');
		var input = $('<input type="text" />').attr('ptag', name).val(val);
		func.append(paramName).append(mao).append(input).append("<br/>");
	});
	func.append('<div class="submit">提交</div>');
	manager.append(func);

}

function attach() {
	$('.manager>.title,.func>.funcName').live('click', function(e) {
		var node = $(this);
		toggle(node);
		return false;
	});

	function toggle(node) {
		var isHide = !! node.data('isHide');
		node.data('isHide', !isHide);
		node.siblings().css('display', isHide ? 'block' : 'none');
	}

	$('.submit').live('click', function(e) {
		var url = opurl;
		var data = getData($(this).parent('.func'));
		var callback = function(data) {
				if(data.flag) {
					console.log(data.data);
					alert("success");
					//disp.text(data.data);
				} else {
					alert(data.msg);
				}

			};

		$.post(url, data, callback);

		function getData(node) {
			var rst = {};
			// op
			rst.op = node.find('.funcName').attr('ptag');
			// param
			node.find('input').each(function(i, n) {
				var ptag = $(n).attr('ptag');
				var val = $(n).val();
				rst[ptag] = val;
			});
			return rst;
		}
	});

}